package com.github.cbyzzy;

import com.github.cbyzzy.common.CommonKit;
import com.github.cbyzzy.common.DeviceTypeEnum;
import com.github.cbyzzy.common.FMSGCallBack_V31_1;
import com.github.cbyzzy.criteria.HTCriteria;
import com.github.cbyzzy.model.entity.Camera;
import com.github.cbyzzy.model.mapper.CameraMapper;
import com.github.cbyzzy.service.CameraService;
import com.github.cbyzzy.service.HikCameraService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationBeanNameGenerator;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import tk.mybatis.spring.annotation.MapperScan;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;

@SpringBootApplication
@EnableCaching
@EnableScheduling
@ServletComponentScan
@EnableAsync
@ComponentScan(nameGenerator = CameraApplication.SpringBeanNameGenerator.class)
@MapperScan("com.**.mapper")
public class CameraApplication implements ApplicationRunner {

	@Autowired
	private FMSGCallBack_V31_1 fmsgCallBackV311;

	@Autowired
	private CameraService cameraService;

	@Override
	public void run(ApplicationArguments args) throws Exception {
		HTCriteria<Camera> criteria = HTCriteria.getInstance(Camera.class);
		criteria.andEqualTo(Camera::getType, DeviceTypeEnum.CAMERA_TYPE_CAM.getCode());
		List<Camera> cameras = cameraService.find(criteria);
		for (Camera camera:cameras){
			fmsgCallBackV311.start(camera);
		}

	}


	public static class SpringBeanNameGenerator extends AnnotationBeanNameGenerator {
		@Override
		protected String buildDefaultBeanName(BeanDefinition definition) {
			return definition.getBeanClassName();
		}
	}

	public static void main(String[] args) {
		ConfigurableApplicationContext applicationContext = SpringApplication.run(CameraApplication.class, args);
		Environment env = applicationContext.getEnvironment();
		String ip="";
		try {
			ip = InetAddress.getLocalHost().getHostAddress();
		}catch (UnknownHostException e){
		}
		String port = env.getProperty("server.port");
		boolean swaggerEnable = true;
//        try
//        {
//            swaggerEnable = env.getProperty("swagger.enable", Boolean.class);
//        }catch (Exception e){
//            e.printStackTrace();
//        }
		String contextPath = env.getProperty("server.servlet.context-path");
		contextPath= StringUtils.isBlank(contextPath)?"/":contextPath;
		StringBuilder sb=new StringBuilder(20);
		sb.append("\n----------------------------------------------------------\n\t");
		sb.append("视频服务模块 服务已运行! 访问地址:\n\t");
		sb.append("本发访问: http://localhost:" + port  + contextPath  +"\n\t");
		if(StringUtils.isNotBlank(ip)) {
			sb.append("网络访问: http://" + ip + ":" + port + contextPath + "\n\t");
		}
		if(swaggerEnable) {
			sb.append("接口文档: http://localhost:" + port + contextPath + "/doc.html\n");
		}else{
			sb.append("接口文档已关闭\n");
		}
		sb.append("----------------------------------------------------------");
		System.out.println(sb.toString());
	}


}
